#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk
from typing import Tuple

from a2_solution import Position
from constants import *


class AbstractGrid(tk.Canvas):
    def __init__(self, master: tk.Tk, rows: int, cols: int, width: int, height: int, **kwargs):
        """
        The parameters rows and cols are the number of rows and columns in the grid, width and height are the
        width and height of the grid (in pixels) and **kwargs signifies that any additional named
        arguments supported by tk.Canvas should also be supported by AbstractGrid.
        Args:
            master:
            rows:
            cols:
            width:
            height:
            **kwargs:
        """
        tk.Canvas.__init__(self, master, width=width, height=height, **kwargs)
        self.pack(side=tk.LEFT)
        self.master = master
        self.rows = rows
        self.cols = cols
        self.width = width
        self.height = height
        self.square_w = int(width/cols)
        self.square_h = int(height/rows)

    def get_bbox(self, position: Position) -> Tuple[int, int, int, int]:
        """
        Returns the bounding box for the (row, column) position;
        this is a tuple containing information about the pixel positions of the edges of the shape,
        in the form (x min, y min, x max, y max).
        Args:
            position:
        Returns:

        """
        x_min = position.get_x() * self.square_w
        y_min = position.get_y() * self.square_h
        x_max = x_min + self.square_w
        y_max = y_min + self.square_h
        return x_min, y_min, x_max, y_max

    def pixel_to_position(self, pixel: Tuple[int, int]) -> Position:
        """
        Converts the (x, y) pixel position (in graphics units) to a (row, column) position.
        Args:
            pixel:

        Returns:

        """
        px, py = pixel
        x = int(px / self.square_w)
        y = int(py / self.square_h)
        return Position(x, y)

    def get_position_center(self, position: Position) -> Tuple[int, int]:
        """
        Gets the graphics coordinates for the center of the cell at the given (row, column) position.
        Args:
            position:

        Returns:

        """
        x_min, y_min, x_max, y_max = self.get_bbox(position)
        return int((x_max + x_min) / 2), int((y_max + y_min) / 2)

    def annotate_position(self, position: Position, text: str):
        """
        Annotates the center of the cell at the given (row, column) position with the provided text.
        Args:
            position:
            text:

        Returns:

        """

        pass # have no idea how to use
        # x, y = self.get_position_center(position)

        # label = tk.Label(self, text=text, font=FONT_TEXT
        # label.place(x=x, y=y)
